#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


int main(int argc,char *argv[]) {
    size_t len;
    off_t offset;
    int fd,ap,j;

    ssize_t numRead,numWritten;

    if (argc < 3   || strcmp(argv[1],"--help") == 0) {
        printf("%s file ....\n");
        exit(-1);
    }

    fd = open(argv[1],O_RDWR | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH | S_IROTH);
    if (fd == -1) {
        printf("%s open error\n",argv[1]);
        exit(-1);
    }

    for (ap = 2;ap < argc;ap++) {
        switch (argv[ap][0])
        {
        case 'w':
        case 'W':
          numWritten = write(fd,&argv[ap][1],strlen(&argv[ap][1]));
          if (numWritten == -1) {
             printf("wriet error\n");
             exit(-1);
          }

        break;
        case 's': // ap = 2时，change the file offset
          if (sscanf(argv[ap],"s%ld",&offset)== 1) {
            printf("提取数字成功");
          }else{
                printf("提取数字失败");
                exit(-1);
          }
    
          if (lseek(fd,offset,SEEK_SET) == -1){
            printf("lseek error\n");
            exit(-1);
          }

          break;

        
        default:
            break;
        }
    }




    return 0;
}